//
//  YSPRefreshView.swift
//  Weibo Demo
//
//  Created by 朴裕识 on 15/12/25.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit

class YSPRefreshViewController: UIRefreshControl {
    private let refreshOffset = CGFloat(-60)
    
    //记录箭头是不是应该朝上
    private var isUP = false
    
    override var frame: CGRect {
        didSet {
            if frame.origin.y > 0 {
                return
            }
            //(0,-60)
            if frame.origin.y < refreshOffset && !isUP{
//                print("箭头转上去动画")
                isUP = !isUP
                refreshView.doArrowViewAnimation(isUP)
            }
            //
            else if frame.origin.y > refreshOffset && isUP {
//                print("箭头转下去去动画")
                isUP = !isUP
                refreshView.doArrowViewAnimation(isUP)
            }
            
            if refreshing {
                refreshView.doRotateViewAnimation()
            }
        }
    }
    
    override init() {
        super.init()
        prepareUI()
    }
    
    func prepareUI() {
        addSubview(refreshView)
        refreshView.ff_AlignInner(type: ff_AlignType.CenterCenter, referView: self, size: nil)
        refreshView.rotateView.hidden = true
        refreshView.rotateViewLbl.hidden = true
    }
    
    override func endRefreshing() {
        super.endRefreshing()
        refreshView.endLoading()
    }
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
 
    private lazy var refreshView: YSPRefreshView = YSPRefreshView.refreshView()
}

class YSPRefreshView: UIView {
    

    @IBOutlet weak var rotateViewLbl: UILabel!
    @IBOutlet weak var arrowViewLbl: UILabel!
    @IBOutlet weak var arrowView: UIImageView!
    
    @IBOutlet weak var rotateView: UIImageView!
    
    
    func doArrowViewAnimation(should: Bool) {
        
        arrowView.hidden = false
        arrowViewLbl.hidden = false
        
        UIView.animateWithDuration(0.25) { () -> Void in
            self.arrowView.transform = should ? CGAffineTransformMakeRotation(CGFloat(M_PI - 0.001)) : CGAffineTransformIdentity
        }
    }
    
    //开始加载数据时的动画
    func doRotateViewAnimation() {
        arrowView.hidden = true
        arrowViewLbl.hidden = true
        rotateView.hidden = false
        rotateViewLbl.hidden = false
        
        
        let animationKey = "animationKey"
        if rotateView.layer.animationForKey(animationKey) != nil {
            return
        }
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.duration = 1
        animation.toValue = 2 * M_PI
        animation.repeatCount = MAXFLOAT
        animation.removedOnCompletion = false
        
        rotateView.layer.addAnimation(animation, forKey: animationKey)
    }
    
    //结束加载数据时的动画
    func endLoading() {
        rotateView.hidden = true
        rotateViewLbl.hidden = true
        rotateView.layer.removeAllAnimations()
    }
    
    
    class func refreshView() -> YSPRefreshView {
        let view = NSBundle.mainBundle().loadNibNamed("YSPRefreshView", owner: nil, options: nil).last as! YSPRefreshView
        return view
    }
}
